<template>
  <a-modal
    title="编辑指标分类"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div style="height: 400px; overflow-y: auto">
      <!-- <a-spin :spinning="confirmLoading"> -->
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="父级菜单" has-feedback>
          <a-tree-select
            v-decorator="['pid', { rules: [{ required: true, message: '请选择上级指标分类！' }] }]"
            show-search
            style="width: 350px"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            placeholder="Please select"
            :treeData="typeTreeData"
          >
          </a-tree-select>
        </a-form-item>

        <a-form-item label="指标名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入指标名称"
            v-decorator="['name', { rules: [{ required: true, message: '请输入指标名称！' }] }]"
            style="width: 350px"
          />
        </a-form-item>
        <!-- <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入编码" v-decorator="['code', { rules: [{ required: true, message: '请输入编码！' }] }]" />
        </a-form-item> -->
        <!-- <a-form-item label="指标级别" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
            <a-input-number
              placeholder="请输入级别"
              v-decorator="['level', { rules: [{ required: true, message: '请输入级别' }] }]"
              @change="onChangelevel"
              style="width: 350px"
            />
          </a-form-item>
          <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
            <a-input-number
              placeholder="请输入排序"
              v-decorator="['orderList', { rules: [{ required: true, message: '请输入排序！' }] }]"
              @change="onChange"
              :min="0"
              style="width: 350px"
            />
          </a-form-item> -->
        <a-form-item label="指标类型" v-if="pid != 0" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <!-- <a-input
            placeholder="请输入指标类型"
            v-decorator="['type', { rules: [{ required: true, message: '请输入指标类型！' }] }]"
          /> -->
          <a-select
            v-decorator="['type', { rules: [{ required: true, message: '请选择指标类型！' }] }]"
            aria-selected="全部"
            style="width: 350px"
            placeholder="请选择指标类型！"
          >
            <a-select-option value="point"> point </a-select-option>
            <a-select-option value="line"> line </a-select-option>
            <a-select-option value="cover"> cover </a-select-option>
            <a-select-option value="grid"> gird </a-select-option>
          </a-select>
        </a-form-item>
        <!-- <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-switch
              v-decorator="['isEnable', { rules: [{ required: true, message: '请选择状态！' }] }]"
              checked-children="是"
              un-checked-children="否"
            />
          </a-form-item> -->
        <!-- <a-form-item label="是否为默认指标" :labelCol="halfLabelCol" :wrapperCol="wrapperCol">
          <a-switch
            v-decorator="['isDefaultFT', { valuePropName: 'checked' }]"
            checked-children="是"
            un-checked-children="否"
          />
        </a-form-item>
        <a-form-item label="是否为常用指标" :labelCol="halfLabelCol" :wrapperCol="halfWrapperCol">
          <a-switch
            v-decorator="['isCommonFT', { valuePropName: 'checked' }]"
            checked-children="是"
            un-checked-children="否"
          />
        </a-form-item> -->
        <a-row>
          <a-col :span="13">
            <a-form-item label="是否为默认指标" :labelCol="halfLabelCol" :wrapperCol="halfWrapperCol">
              <a-switch
                v-decorator="['isDefaultFT', { valuePropName: 'checked' }]"
                checked-children="是"
                un-checked-children="否"
              />
            </a-form-item>
          </a-col>
          <a-col :span="13">
            <a-form-item label="是否为常用指标" :labelCol="halfLabelCol" :wrapperCol="halfWrapperCol">
              <a-switch
                v-decorator="['isCommonFT', { valuePropName: 'checked' }]"
                checked-children="是"
                un-checked-children="否"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <!-- <a-form-item
          label="父id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入父id" v-decorator="['pid', {rules: [{required: true, message: '请输入父id！'}]}]" />
        </a-form-item> -->
        <!-- <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['pids', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item> -->
        <!-- <a-form-item
          label="是否父分组 Y/N"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否父分组 Y/N" v-decorator="['isParent', {rules: [{required: true, message: '请输入是否父分组 Y/N！'}]}]" />
        </a-form-item> -->
        <!-- <a-form-item
          label="是否默认指标 Y/N"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否默认指标 Y/N" v-decorator="['isDefault', {rules: [{required: true, message: '请输入是否默认指标 Y/N！'}]}]" />
        </a-form-item> -->
        <a-form-item label="指标颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <colorPicker v-model="record.color" />
        </a-form-item>
        <a-form-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea placeholder="请输入说明" :auto-size="{ minRows: 5, maxRows: 10 }" v-decorator="['description']" />
        </a-form-item>

        <!-- <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['source', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item> -->
      </a-form>
      <!-- </a-spin> -->
    </div>
  </a-modal>
</template>

<script>
import { biIndexTypeEdit, biIndexTypeSelectTree } from '@/api/modular/main/biindextype/biIndexTypeManage'
import vcolorpicker from 'vcolorpicker'
export default {
  components: {
    vcolorpicker,
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 },
      },
      halfLabelCol: { xs: { span: 48 }, sm: { span: 10 } },
      halfWrapperCol: { xs: { span: 48 }, sm: { span: 10 } },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      typeTreeData: [],
      pid: '',
      record: {},
    }
  },
  created() {
    this.loadSelectTree()
  },
  methods: {
    // 上级父类选择器（tree）
    loadSelectTree() {
      biIndexTypeSelectTree({}).then((res) => {
        if (res.success) {
          this.typeTreeData = [
            {
              id: '-1',
              parentId: '0',
              title: '一级指标',
              value: '0',
              pid: '0',
              children: res.data,
            },
          ]
        }
      })
    },
    // 初始化方法
    edit(record) {
      this.record = record
      this.record.color = record.color === null || record.color === 'null' ? '#000000' : record.color
      this.pid = record.pid
      this.loadSelectTree()
      this.visible = true
      setTimeout(() => {
        if (this.pid == 0) {
          this.form.setFieldsValue({
            id: record.id,
            name: record.name,
            pid: record.pid,
            description: record.description,
            isDefaultFT: record.isDefaultFT,
            isCommonFT: record.isCommonFT,
          })
        }else{
          this.form.setFieldsValue({
            id: record.id,
            name: record.name,
            type: record.type,
            pid: record.pid,
            description: record.description,
            isDefaultFT: record.isDefaultFT,
            isCommonFT: record.isCommonFT,
          })
        }
        
      }, 300)
    },
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          biIndexTypeEdit(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok')
                this.handleCancel()
              } else {
                this.$message.error(res.message) //  + res.message
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
      this.loadSelectTree()
    },
    onChange(orderList) {
      console.log('changed', orderList)
    },
    onChangelevel(level) {
      console.log('changed', level)
    },
  },
}
</script>
